package com.aaa.tars.controller;

import com.aaa.tars.entity.Visitors;
import com.aaa.tars.service.CodesService;
import com.aaa.tars.service.VisitorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @FileName: AuthController
 * @Description:
 * @Author: 李宗盛
 * @CreateTime: 2025/9/12 21:11
 * @Version: 17
 */
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private CodesService codesService;
    @Autowired
    private VisitorsService visitorsService;
    //发送验证码
    @PostMapping("/sendCode")
    public Map<String, Object>sendCode(@RequestParam String phoneNumber){
        Map<String ,Object> result=new HashMap<>();
        boolean success =codesService.generateAndSendCode(phoneNumber);
        result.put("success",success);
        result.put("message",success ?"验证码发送成功":"验证码发送失败");
        return  result;
    }
    //用户注册
    @PostMapping("/register")
    public  Map<String ,Object> register(@RequestBody Visitors visitors,
                                         @RequestParam String verificationCode){
        Map<String ,Object> result =new HashMap<>();
        boolean success =visitorsService.register(visitors,verificationCode);
        result.put("success", success);
        result.put("message", success ? "注册成功" : "注册失败，请检查验证码或手机号是否已注册");
        return result;
    }
    //用户登录
    @PostMapping("/login")
    public  Map<String ,Object> login(@RequestParam String phoneNumber,
                                      @RequestParam String password){
        Map<String ,Object> result =new HashMap<>();
        Visitors visitors=visitorsService.login(phoneNumber,password);
        if (visitors!=null){
            result.put("success",true);
            result.put("message","登录成功");
            result.put("data",visitors);
        }else {
            result.put("success",false);
            result.put("message","用户名或密码错误");
        }
        return result;
    }
    //获取用户信息
    @GetMapping("/userInfo")
    public  Map<String ,Object>getUserInfo(@RequestParam String phoneNumber){
        Map<String ,Object>result =new HashMap<>();
        Visitors visitors=visitorsService.findByPhoneNumber(phoneNumber);
        if (visitors!=null){
            result.put("success", true);
            result.put("data", visitors);
        } else {
            result.put("success", false);
            result.put("message", "用户不存在");
        }
        return  result;
    }
    @PostMapping("/verifyCode")
    public Map<String, Object> verifyCode(@RequestParam String phoneNumber,
                                          @RequestParam String code) {
        Map<String, Object> result = new HashMap<>();
        boolean success = codesService.verifyCode(phoneNumber, code);
        result.put("success", success);
        result.put("message", success ? "验证码验证成功" : "验证码错误或已过期");
        result.put("data", success); // 返回验证结果
        return result;
    }
}